package com.nju.edu.erp.service.hr;

import com.nju.edu.erp.dao.SaleSheetDao;
import com.nju.edu.erp.dao.StuffDao;
import com.nju.edu.erp.model.po.StuffPO;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MonthPayWithCommision implements GetSalaryWithTax {


    private final StuffDao stuffDao;
    private final SaleSheetDao saleSheetDao;
    MonthPayWithCommision(StuffDao stuffDao,SaleSheetDao saleSheetDao){
        this.stuffDao=stuffDao;
        this.saleSheetDao=saleSheetDao;
    }


    @Override
    public BigDecimal getSalaryWithTax(String name) {
        StuffPO stuffPO=stuffDao.getStuffByName(name);
        //基本工资+职位工资
        BigDecimal salaryWithTax=stuffPO.getBaseSalary().add(stuffPO.getJobSalary());
        //获取提成 先计算本月1号和30号（假定每个月只有30天）
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar=Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH)+1;
        String beginDateStr=String.format("%4d-%2d-01 00:00:00",year,month);
        String endDateStr=String.format("%4d-%2d-30 23:59:59",year,month);
        Date beginTime = null;
        Date endTime = null;
        try {
            beginTime =dateFormat.parse(beginDateStr);
            endTime=dateFormat.parse(endDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //+提成
        BigDecimal commission=saleSheetDao.getTotalAmountOfSalesmanByTime(name,beginTime,endTime);
        salaryWithTax=salaryWithTax.add(commission.multiply(BigDecimal.valueOf(0.05)));
        //计算出勤率
        BigDecimal attendanceTime= BigDecimal.valueOf(stuffPO.getCheckIn());
        BigDecimal attendanceRate = attendanceTime.divide(BigDecimal.valueOf(30));
        salaryWithTax=salaryWithTax.multiply(attendanceRate);
        return salaryWithTax;
    }
}
